<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_seckill_task_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image','price'));
		$v['image']=$good['image'];
		$v['timeid']=$v['timeid'].'点';
		$v['title']=$good['title'];
		$v['oldprice']=$good['price'];
		$v['room']=pdo_getcolumn('jy_seckill_task_room',array('id'=>$v['roomid']),'title');
		$v['task']=pdo_getcolumn('jy_seckill_task',array('id'=>$v['taskid']),'title');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'task','title'=>'专题','type'=>'string'),	
			array('name'=>'room','title'=>'会场','type'=>'string'),	
			array('name'=>'timeid','title'=>'时间段','type'=>'string'),	
			array('name'=>'price','title'=>'秒杀价格','type'=>'string'),
			array('name'=>'oldprice','title'=>'原价','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='task'){
	if($_RQ['id']){
		$taskid=$_SESSION['taskid']=$_RQ['id'];
	}else{
		$taskid=$_SESSION['taskid'];
	}
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'taskid'=>$taskid);
    $fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_seckill_task_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image','price'));
		$v['image']=$good['image'];
		$v['timeid']=$v['timeid'].'点';
		$v['title']=$good['title'];
		$v['oldprice']=$good['price'];
		$v['room']=pdo_getcolumn('jy_seckill_task_room',array('id'=>$v['roomid']),'title');
		$v['task']=pdo_getcolumn('jy_seckill_task',array('id'=>$v['taskid']),'title');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;	
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'task','title'=>'商品管理'),
			array('op'=>'edit','title'=>'商品添加'),
		),
		'list'=>array('data'=>'list','tip'=>'要添加商品,请移步《会场》->编辑添加商品','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'task','title'=>'专题','type'=>'string'),	
			array('name'=>'room','title'=>'会场','type'=>'string'),	
			array('name'=>'timeid','title'=>'时间段','type'=>'string'),	
			array('name'=>'price','title'=>'秒杀价格','type'=>'string'),
			array('name'=>'oldprice','title'=>'原价','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='room'){
	if($_RQ['id']){
		$roomid=$_SESSION['roomid']=$_RQ['id'];
	}else{
		$roomid=$_SESSION['roomid'];
	}
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$roomid);
    $fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_seckill_task_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image','price'));
		$v['image']=$good['image'];
		$v['timeid']=$v['timeid'].'点';
		$v['title']=$good['title'];
		$v['oldprice']=$good['price'];
		$v['room']=pdo_getcolumn('jy_seckill_task_room',array('id'=>$v['roomid']),'title');
		$v['task']=pdo_getcolumn('jy_seckill_task',array('id'=>$v['taskid']),'title');
		$v['isoptions']=pdo_count('jy_weishop_goods_option',array('goodsid'=>$v['goodsid']),'id');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;	
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'room','title'=>'商品管理'),
			array('op'=>'edit','title'=>'商品添加'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'task','title'=>'专题','type'=>'string'),	
			array('name'=>'room','title'=>'会场','type'=>'string'),	
			array('name'=>'timeid','title'=>'时间段','type'=>'string'),	
			array('name'=>'price','title'=>'秒杀价格','type'=>'string'),
			array('name'=>'oldprice','title'=>'原价','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'options','title'=>'设置规格','show'=>array('isoptions >'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_seckill_task_goods',array('id'=>$id));
		$item['goods']=pdo_getall('jy_seckill_task_goods',array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$id,'taskid'=>$_SESSION['taskid']));
	}else{
		$item=array();
	}
    $return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'会场设置',
		'tabs'=>array(
			array('op'=>'room','title'=>'商品管理'),
			array('op'=>'edit','title'=>'商品添加'),
		),
		'edit'=>array(
			array('name'=>'timeid','title'=>'选择时间段','type'=>'radio','options'=>'times','help'=>'这里只显示关联专题开放的时间点，如果显示空，说明专题没有设置时间点'),
			array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
			array('name'=>'price','title'=>'秒杀价格','type'=>'string'),
			array('name'=>'total','title'=>'库存','type'=>'string'),
			array('name'=>'maxbuy','title'=>'限购','type'=>'string'),
			array('name'=>'commission1','title'=>'一级分销','type'=>'string'),
			array('name'=>'commission2','title'=>'二级分销','type'=>'string'),
		)
	);
	$times=pdo_getall('jy_seckill_task_time',array('taskid'=>$_SESSION['taskid'],'uniacid'=>$_SESSION['uniacid']),'time','time');
	foreach($times as $k=>&$tv){
		$return['times'][$k]=$k.'点';
	}	
	$return['diypage']=array('0'=>'系统默认','1'=>'整点秒杀');
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['oldshow']=array('0'=>'不允许','1'=>'允许');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	$upgoods=array();
	$upgoods['goodsid']=$postdata['goodsid'];
	$upgoods['price']=$postdata['price'];
	$upgoods['total']=$postdata['total'];
	$upgoods['maxbuy']=$postdata['maxbuy'];
	$upgoods['commission1']=$postdata['commission1'];
	$upgoods['commission2']=$postdata['commission2'];
	$upgoods['commission3']=$postdata['commission3'];
	$upgoods['timeid']=$postdata['timeid'];
	if($id){
		$oldgoodsid=pdo_getcolumn('jy_seckill_task_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'goodsid');	
		$isactive=pdo_getcolumn('jy_weishop_goods',array('id'=>$upgoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'active_plugin');
		if($upgoods['goodsid']!=$oldgoodsid || !$isactive){
			model('jy_weishop/activity')->create_active($upgoods['goodsid'],$id,$this->plugin_name);
		}
		pdo_update('jy_seckill_task_goods',$upgoods,array('id'=>$id));
	}else{
		$isactive=pdo_getcolumn('jy_weishop_goods',array('id'=>$upgoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'active_plugin');
		if($isactive!=$this->plugin_name){
			if(model('jy_weishop/activity')->check_active($upgoods['goodsid'])){
					exi('商品已存在活动，请勿重复添加！','error');
			}
		}
		$upgoods['taskid']=$_SESSION['taskid'];
		$upgoods['roomid']=$_SESSION['roomid'];
		$upgoods['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_seckill_task_goods',$upgoods);
		$active_id=pdo_insertid();
		model('jy_weishop/activity')->create_active($upgoods['goodsid'],$active_id,$this->plugin_name);
	}
	$this->model('model')->setTaskCache($_SESSION['taskid']);
	exi('设置成功！','','room');
}
if($_RQ['op']=='options'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$this->session('tgoodsid',$id);
	$goods=pdo_get('jy_seckill_task_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$optionsset=json_decode($goods['optionsset'],true);
	$list=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($list as &$v){
			$specs=$v['specs'];
			$specs=explode("_",$specs);
			$selespename='';
			foreach($specs as &$spec){
				$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				$selespename .=$title.',';
			}
			$v['selespename']=$selespename;
			$v['mprice']=$goods['price'];
			foreach($optionsset as $ov){
				if($ov['id']==$v['id']){
					$v['mprice']=$ov['price'];
					break;
				}
			}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'room','title'=>'返回商品管理'),
		),
		'list'=>array('data'=>'list','tip'=>'这里设置已有的规格，只能设置秒杀价格','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'selespename','title'=>'规格名称','type'=>'string'),
			array('name'=>'price','title'=>'商品原价','type'=>'string'),
			array('name'=>'mprice','title'=>'秒杀价格(点击编辑↓)','type'=>'string','set'=>'optionprice'),	
		)),
	);
	$return['htmls']=$htmls;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='optionprice'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择规格！','error');
	}
	$tgoodsid=$this->session('tgoodsid');
	$goods=pdo_get('jy_seckill_task_goods',array('id'=>$tgoodsid,'uniacid'=>$_SESSION['uniacid']));
	$optionsset=json_decode($goods['optionsset'],true);
	$post=get_postdata();
	if(isset($post['mprice'])){
		if($optionsset[$id]){
			$optionsset[$id]['price']=$post['mprice'];
		}else{
			$optionsset[$id]=array(
				'id'=>$id,
				'price'=>$post['mprice']
			);
		}
		$this->pdo_update('task_goods',array('optionsset'=>ijson_encode($optionsset)),array('id'=>$tgoodsid,'uniacid'=>$_SESSION['uniacid']));
		$this->model('model')->setTaskCache($_SESSION['taskid']);
	}
	exi('修改成功');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_seckill_task_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$goodsid=pdo_getcolumn('jy_seckill_task_goods',array('id'=>$id),'goodsid');
		pdo_update('jy_weishop_goods',array('active_plugin'=>''),array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_seckill_task_goods',array('id'=>$id));
				$goodsid=pdo_getcolumn('jy_seckill_task_goods',array('id'=>$id),'goodsid');
				pdo_update('jy_weishop_goods',array('active_plugin'=>''),array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}	
	$this->model('model')->setTaskCache($_SESSION['taskid']);
	exi('删除成功！');
}
?>